<?php 
//步骤一     准备测试数据    搭建页面
$all_data = array(
    1 => array("1学PHP，冲击月薪10000+你也可以！","......"),
    2 => array("2学PHP，冲击月薪10000+你也可以！","......"),
    3 => array("3学PHP，冲击月薪10000+你也可以！","......"),
    4 => array("4学PHP，冲击月薪10000+你也可以！","......"),
    5 => array("5学PHP，冲击月薪10000+你也可以！","......")
);

//步骤2：设置分页
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$page = max($page,1);
$page = min($page,count($all_data));
$pre_page = $page -1;
$next_page = $page +1;

//  步骤3   历史记录  保存cookie字符串   显示cookie_arr    key  /页码page
$cookie_arr = array();
if ($_COOKIE["history"]){ // （2）有浏览历史
    $cookie_arr = explode(",", $_COOKIE["history"]);
    foreach ($cookie_arr as $k => $v){
        if ($v == $page){
         unset($cookie_arr[$k]);   
        }
    }
    $cookie_arr[] = $page;
    
    setcookie("history",implode(",",$cookie_arr));
    
    if (count($cookie_arr)>2){
        array_shift($cookie_arr);
    }
}
else{//首次进入
    setcookie("history",$page);
    $cookie_arr[] = $page;
}
krsort($cookie_arr);

if(isset($_GET['action'])){
    if($_GET['action'] == "clear"){
        setcookie("history","",time()-1);
        $cookie_arr = array();
    }
}
// echo "<pre>";
// print_r($cookie_arr);
// echo "<pre>";
?>
<!DOCTYPE>
<html>
<head>
<meta charset="utf-8">
<title>历史记录</title>
</head>
<style>
    body{margin:0;padding:0;}
    .box{margin:20px;border:1px solid #C8D8F2;background:pink;}
    .box .content{margin:20px;}
    .box .page{margin-left:20px;}
    .box .history{margin:20px;}
    .box a{color:#0033ff}
</style>
<body>
	<div class="box">
		<div class="content">
			<h1><?=$all_data[$page][0]?></h1>
			<p><?=$all_data[$page][1]?></p>
		</div>
		<div class="page">
			<a href="?page=<?=$pre_page?>">上一篇</a>
			<a href="?page=<?=$next_page?>">下一篇</a>
		</div>
		<div class="history">
			浏览历史：（<a href="?action=clear">清除历史</a>）
			<ul>
			<?php 
			foreach ($cookie_arr as $k => $v)
			{
			?>
				<li><?= $all_data[$v][0]?></li>
			<?php 
			}
			?>
			</ul>
		</div>
	</div>

</body>
</html>
